Repository navigation
level: don't read a key name as a line's level - #38
Conversation
A container log line has no level from its source, so the line collector
guesses one from the raw text. For a JSON line that has no level key, that
guess came from the key names: an access log's {"status":200,"error":""}
read as an error, because "error" is a key. JSON parsing kept the guess,
since it found no level key to override it with.
The collector now records that a level was guessed. When the line turns
out to be a JSON object without a level key, the parser guesses again from
the JSON message field alone ({"msg":"error: db down"} is still an error),
or leaves the level unknown. A level the source reports, such as a journald
priority, is kept. Both parseJson modes behave the same.
There was a problem hiding this comment.
Code Review
This pull request introduces a levelGuessed flag to track whether a log level was guessed from the content. In parser.go, this flag is used to refine the log level for JSON lines that lack an explicit level key, preventing false-positive level guesses from key names (such as "error"). A performance optimization was suggested to avoid the expensive JSON decoding in parser.go when msg.Level is already LevelUnknown.
mayankpande88
left a comment
There was a problem hiding this comment.
I ran sample lines through NewParser on this head. The fix works for the access-log case, but it turns some lines that used to be error into unknown, and the two parseJson modes still give different levels. Details are inline.
One more point, outside the diff (level.go, GuessLevel): the root cause is that GuessLevel reads a key name ("error":, error=) as a level word, and this PR patches only single-line JSON in Parser.inc. These lines are still error in both modes:
- logfmt:
method=GET uri=/healthz status=200 error="", becauseParseStructuredLogreturnsGuessLevel(line)as its level - multiline JSON:
{"method":"GET","status":200,"error":""}followed by a continuation line
If GuessLevel skipped a token followed by ": or = (a key, not a value), every format would be covered, and the re-guess block here might not be needed.
…e key set
GuessLevel now skips a key, a quoted word followed by a colon ("error":)
or a word=value pair (error=""), instead of reading it as a level word.
That fixes every format, not just single-line JSON: logfmt and multiline
JSON lines with an empty error field are no longer errors.
This replaces the re-guess from the JSON message field and the
levelGuessed flag, which turned lines whose level sat outside a level
key into unknown and decoded the line a second time.
structuredFromJSON reads the level from the keys jsonLogFromFields
reads (@l, severity_text, numeric levels) before patternLevelKeys, so
a line gets the same level with and without parseJson. A numeric level
is accepted however it is written (50, 50.0, 5e1).
|
@mayankpande88 Thanks, I took the I checked plain-text formats that put brackets after the level word or quote a phrase that ends in one: |
Summary
A line with no level key but an
errorkey was emitted as an error. Go access logs (Echo, Gin) are the common case: every{"method":"GET","uri":"/healthz","status":200,"error":""}health check came out aserror, and so did logfmt... status=200 error="".The cause was
GuessLevel, which read the key"error"as the word error.GuessLevelnow skips a key name: a quoted word directly followed by:("error":), or akey=valuetoken (error=""). Values still count, so{"type":"error"},{"msg":"error: db down"}andERROR[req=abc] ...keep their level. The change is inGuessLevel, so it covers single-line JSON, logfmt and multiline JSON alike.parseJsonmodes now read the same level keys.structuredFromJSONreads the keysjsonLogFromFieldsreads (@l,severity_text, numeric pino levels) before the olderpatternLevelKeys, so pino{"level":50}and Serilog{"@l":"Error"}areerrorwithparseJsonoff too. A numeric level is accepted however it is written (50,50.0,5e1).Trade-off: in plain text, a quoted word followed by a colon (
"ERROR": none) now reads as a key, not a level. A dotted logfmt key (error.code=5) is still read aserror, as onmain.No API change. Each line is decoded once, as on
main.How to verify
go test -count=1 -run 'TestParserLevelNotFromKeyNames|TestGuessLevelSkipsKeys' ./....TestParserLevelNotFromKeyNamesruns 16 lines throughNewParserunder bothparseJsonmodes. They include the two health checks, logfmt and multilineerrorkeys, nested and non-level-key levels, and pino,50.0,@landseverity_textlevels. Onmain, theerror-key cases fail, and so do the numeric,@landseverity_textcases withparseJsonoff.What was run
go vet ./...,go build -mod=readonly ./...,go test -count=1 ./...pass.-racewas not run locally (no cgo on the dev box); CI runs it.GuessLevelwere compared on about 45 plain-text lines (glog/klog, nginx, apache, MySQL, Python logging, logback, dockerlevel=, UTF-8). They differ only on the intended key skips.